package se19team1e.gui.awt;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import se19team1e.controller.Application;
import se19team1e.controller.DeleteJobAdvertController;
import se19team1e.entity.CompanyDTO;
import se19team1e.entity.DepartmentDTO;
import se19team1e.entity.JobAdvertDTO;
import se19team1e.exception.DataAccessException;
import se19team1e.gui.DeleteJobAdvertForm;

/**
 * 
 * A GUI form that allows the advertiser to delete job adverts. 
 * 
 * @author Toh Chen Lim
 *
 */
@SuppressWarnings("serial")
public class AWTDeleteJobAdvertForm extends AWTBaseScreen implements DeleteJobAdvertForm {

    /**
     * A reference to the current owning controller of this form.
     */
    private DeleteJobAdvertController controller;

    /**
     * The job advert input panel.
     */
    private final JobAdvertInputPanel jobAdvertInputPanel;
    
    /**
     * An awt button to submit login credentials.
     */
    private final Button deleteButton;

    /**
     * An awt button to cancel job advert.
     */
    private final Button cancelButton;

    /**
     * Constructs a delete job advert form, initializing with the application instance.
     * @param application Reference to the current application instance.
     */
    @SuppressWarnings("deprecation")
    public AWTDeleteJobAdvertForm( Application application ) {
        super(application);

        //this.setBounds(150, 50, 575, 659 );  
        // initialize layout
        getContentPane().setLayout( new BorderLayout() );
        
        // create panel to hold all sub panels
        Panel mainPanel = new Panel();
        mainPanel.setLayout(new BorderLayout());
        
        // create panel to hold text field
        this.jobAdvertInputPanel = new JobAdvertInputPanel();
        
        // add panel to center panel
        mainPanel.add( this.jobAdvertInputPanel,BorderLayout.CENTER );
        
        // create panel to hold buttons
        Panel buttonPanel = new Panel();

        // add panel to center panel
        mainPanel.add( buttonPanel, BorderLayout.SOUTH );
        
        // create and add submit button
        this.deleteButton = new Button( "Confirm Delete" );
        // add callback for submit button
        this.deleteButton.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed( ActionEvent e ) {
                try {
					deleteJobAdvert();
				} catch (DataAccessException e1) {
					e1.printStackTrace();
				}
            }
        });

        // create and add cancel button
        this.cancelButton = new Button( "Cancel" );
        // add callback for cancel button
        this.cancelButton.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed( ActionEvent e ) {
                try {
                    showJobAdvertSearchForm();
                } catch (DataAccessException e1) {
                    e1.printStackTrace();
                }
            }
        });
        
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        buttonPanel.add( this.deleteButton );
        buttonPanel.add( this.cancelButton );
        
        // add panel to frame
        getContentPane().add( mainPanel);
        
        // pack the layout
        this.pack();
        
        // hide the frame
        this.hide();
    }

    /* (non-Javadoc)
     * @see se19team1e.IBaseScreen#updateAvailableActions()
     */
    @Override
    public void updateAvailableActions() {
        this.updateAvailableMenuActions();

        // ensure delete button is enabled
        deleteButton.setEnabled( true );
    }

    /* (non-Javadoc)
     * @see se19team1e.gui.DeleteJobAdvertForm#initialize(se19team1e.entity.JobAdvertDTO, se19team1e.controller.DeleteJobAdvertController, java.util.List, java.util.List)
     */
    @Override
    public void initialize(JobAdvertDTO jobAdvertDTO, DeleteJobAdvertController controller, List<CompanyDTO> companies, List<DepartmentDTO> departments ) {
        this.controller = controller;
        
        // initialize the form
        this.jobAdvertInputPanel.initialize(jobAdvertDTO, companies, departments );
        
        // set the inputs to read-only
        this.jobAdvertInputPanel.setReadOnly();
    }

    /**
     * Triggers a deletion of the current job advert.
     * @throws DataAccessException 
     */
    public void deleteJobAdvert() throws DataAccessException {
        // no input to validate

        // delegate to controller
    	JobAdvertDTO jobAdvert=new JobAdvertDTO();
        this.controller.deleteJobAdvert( this.jobAdvertInputPanel.newJobAdvertDTOFromInputForUpdate(jobAdvert));
    }

    /**
     * Triggers a showing of the job advert search form again.
     */
    public void showJobAdvertSearchForm() throws DataAccessException {
        // delegate to controller
        this.controller.showSearchJobAdvertResultsScreen();
    }

}
